import api from "../../../common/api.js";
import p from "../../../utils/platform.js";
import util from "../../../utils/util.js";
import { phoneNumberCheck } from "../../../utils/utilsNew.js";
var settingInfo = wx.getStorageSync("settingInfo") ? wx.getStorageSync("settingInfo") : {},countdown = 60;;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    title: '',//标题
    value: '',//input输入值
    verifyCode: '',//验证码
    isShow: true,//获取验证码按钮显示
    lastTime: '',//倒计时
    noSave: true,//判断手机号码是否符合标准
    imgArray:'',//图片
    goPosterList:false,//是否跳转海报列表
    changHeadImg: false, //是否显示修改头像
    changName: false, //是否显示修改姓名
    changTel: false, //是否显示修改电话
    changBrand: false, //是否显示修改品牌
    changAddress: false, //是否显示修改地址
  },
  //获取验证码
  getCode() {
    const that = this;
    api.regSendVerifyCode({ mp: that.data.value }, res => {
      that.settime();
    })
  },
  //输入验证码
  getVerifyCodeCode(e) {
    this.setData({
      verifyCode: e.detail.value
    })
  },
  chooseimage() {
    var that = this;
    util.updateImages(1,function(res){
      var tempFilePaths = res.tempFilePaths[0];
      util.uploadFiles(tempFilePaths, function (res){
        try {
          var id = JSON.parse(res.data)[0].id;
          api.settingEdit({ avatar: id}, function (res) {
            that.setData({
              imgArray: tempFilePaths,
            })
            if (!that.data.value){
              that.setData({
                imgArray: tempFilePaths,
                noSave:  false
              })
            }else{
              that.setData({
                imgArray: tempFilePaths,
                noSave:  true
              })
            }
          })
          
        } catch (error) {
          
        }
      })
    })
  },
 
  //姓名
  inputVal(e) {
    var that = this;
    that.setData({
      value: e.detail.value
    })
    if (!e.detail.value&&that.data.value=='') {
      that.setData({
        noSave:  false
      })
    }else{
      that.setData({
        noSave: true
      })
    }
  },
  //电话
  inputValTel(e) {
    var that = this;
    that.setData({
      tel: e.detail.value
    })
    if (!e.detail.tel&&that.data.tel=='') {
      that.setData({
        noSave:  false
      })
    }else{
      that.setData({
        noSave: true
      })
    }
  },
  // 品牌
  inputValBrand(e) {
    var that = this;
    that.setData({
      storeName: e.detail.value
    })
    if (!e.detail.storeName&&that.data.storeName=='') {
      that.setData({
        noSave:  false
      })
    }else{
      that.setData({
        noSave: true
      })
    }
  },
// 地址
inputValAddress(e) {
    var that = this;
    that.setData({
      address: e.detail.value
    })
    if (!e.detail.address&&that.data.address=='') {
      that.setData({
        noSave:  false
      })
    }else{
      that.setData({
        noSave: true
      })
    }
  },

  //保存
  save(e) {
    var that = this;
    let data = new Object();
    data.realname = that.data.value;
    data.storeName = that.data.storeName;
    data.storeAdress = that.data.address;
    data.mobile = that.data.tel;
    if (!phoneNumberCheck(data.mobile)) {
      return p.showToast({ title: "请输入正确格式的手机号！", icon: "none", })
    }

    api.settingEdit(data, function (res) {
      if (res.statusCode == 200) {
        if (that.data.goPosterList){
          wx.navigateTo({
            url: '/pages/mine/posterList/posterList',
          })
        }else{
          app.globalData.indexPage.getAllnumber();
          wx.navigateBack({
            delta: 1
          })
        }

        app.publishSessionUserChange()
        
      } else {
        wx.showToast({
          title: '保存失败',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 上传
  upload() {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        const src = res.tempFilePaths[0];
        let value = that.data.value;
        wx.redirectTo({
          url: `/pages/mine/personData/upload/upload?src=${src}&value=${value}`
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this;
    var title = options.title;
      wx.setNavigationBarTitle({
        title: '个人资料',
      })
   
      var settingInfo = wx.getStorageSync("settingInfo")
      that.setData({
        value: settingInfo.name,
        imgArray: settingInfo.imgArray,
        goPosterList: options.goPosterList && options.goPosterList == 1 ? true : false,
        tel: settingInfo.tel,
        address: settingInfo.address,
        storeName: settingInfo.storeName,
      })
      let { avatar } = options;
      let { value } = options;
      if (avatar) {
        that.setData({
          imgArray: avatar,
          value: value
        })
      }
      // if (!settingInfo.imgArray) {
      //   that.setData({
      //     noSave: false
      //   })
      // } else {
      //   that.setData({
      //     noSave: true
      //   })
      // }
      if(wx.getStorageSync('myInformation')=='changHeadimg'){
        this.setData({
          changHeadImg: true
        })
      } else if (wx.getStorageSync('myInformation')=='changName'){
        this.setData({
          changName: true
        })
      }else {
        this.setData({
          changName: true,
          changHeadImg: true,
          changTel: true,
          changBrand: true,
          changAddress: true,
        })
      }
    // }
  },
  //验证码倒计时
  settime: function () {
    var that = this;
    if (countdown == 0) {
      that.setData({
        isShow: true
      })
      countdown = 60;
      return;
    } else {
      that.setData({
        isShow: false,
        lastTime: countdown
      })
      countdown--;
    }
    setTimeout(function () {
      that.settime();
    }, 1000)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
   
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})
